package com.jinzhu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jinzhu.mapper.CategoryMapper;
import com.jinzhu.mapper.custom.CategoryMapperCustom;
import com.jinzhu.pojo.Category;
import com.jinzhu.service.CategoryService;
import com.jinzhu.vo.CategoryVO;
import com.jinzhu.vo.NewItemsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 首页商品分类
 * jinzhu
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    /**
     * 商品信息
     */
    @Autowired
    CategoryMapper categoryMapper;
    /**
     * 自定义Mapper
     */
    @Autowired
    private CategoryMapperCustom categoryMapperCustom;

    /**
     * Propagation.SUPPORTS 如果当前存在事务，则加入该事务；如果当前没有事务，则以非事务的方式继续运行。
     * 查询一级分类 父级分类
     * @author jinzhu
     *
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<Category> queryAllRootLevelCat() {
        List<Category> categories = categoryMapper.selectList(new QueryWrapper<Category>()
                .lambda().eq(Category::getFatherId, 0));
        return categories;
    }


    /**
     * Propagation.SUPPORTS 如果当前存在事务，则加入该事务；如果当前没有事务，则以非事务的方式继续运行。
     * 查询一级分类下的子集二级
     * @author jinzhu
     *
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<CategoryVO> getSubCatList(Integer rootCatId) {
        Map<String, Object> map = new HashMap<>();
        map.put("rootCatId", rootCatId);
        return categoryMapperCustom.getSubCatList(map);
    }

    /**
     * Propagation.SUPPORTS 如果当前存在事务，则加入该事务；如果当前没有事务，则以非事务的方式继续运行。
     * 查询每个一级分类下的最新6条商品数据
     * @param rootCatId
     * @return
     * @author jinzhu
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<NewItemsVO> getSixNewItemsLazy(Integer rootCatId) {
        Map<String, Object> map = new HashMap<>();
        map.put("rootCatId", rootCatId);
        return categoryMapperCustom.getSixNewItemsLazy(map);
    }
}
